package com.straw.hat.dubbo.provider.config;

import org.apache.dubbo.config.*;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.apache.dubbo.config.spring.context.annotation.EnableDubboConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * @company: 鲸灵科技
 * @author: qiyue@webuy.ai
 * @date: 2019/10/3
 * @time: 12:54 下午
 */
@Configuration
public class DubboConfig {

    @Value("${dp.dubbo.application.name}")
    private String applicationName;
    @Value("${dp.dubbo.protocol.port}")
    private Integer localPort;
    @Value("${dp.dubbo.protocol.name}")
    private String  dubboName;
    @Value("${dp.dubbo.registry.address}")
    private String  registerUrl;
    @Value("${dp.dubbo.registry.register}")
    private boolean registerSwitch;
    @Value("${dp.dubbo.registry.group}")
    private String dubboGroup;



    //注册中心类型
    private final String REGISTER_TYPE = "zookeeper";

    private final static Logger logger = LoggerFactory.getLogger(DubboConfig.class);

    @Bean
    @Primary
    public ApplicationConfig applicationConfig() {
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName(applicationName);
        return applicationConfig;
    }

    @Bean
    @Primary
    public ProtocolConfig protocolConfig(){
        ProtocolConfig protocolConfig = new ProtocolConfig();
        protocolConfig.setPort(localPort);
        protocolConfig.setName(dubboName);
        return protocolConfig;
    }

    @Bean
    @Primary
    public RegistryConfig registryConfig() {
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress(registerUrl);
        registryConfig.setProtocol(REGISTER_TYPE);
        return registryConfig;
    }

    @Bean
    @Primary
    public ConsumerConfig consumerConfig() {
        ConsumerConfig consumerConfig = new ConsumerConfig();
        consumerConfig.setGroup(dubboGroup);
        return consumerConfig;
    }

    @Bean
    @Primary
    public ProviderConfig providerConfig() {
        ProviderConfig providerConfig = new ProviderConfig();
        providerConfig.setGroup(dubboGroup);
        return providerConfig;
    }







}
